package com.future.mayi.qsale.activity;

import android.webkit.WebView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseTitleActivity;
import com.future.mayi.utils.HtmlFormat;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProtocolActivity extends BaseTitleActivity {

    public static final String EXTRA_IS_RECYCLER = "extra_is_extra";

    //
//    @BindView(R.id.tv_protocol)
//    TextView tvProtocol;
    @BindView(R.id.web_view)
    WebView webView;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText(isRecyclerFlow() ? "回收协议" : "寄卖协议");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_protocol);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {
        new HttpRequest<DefaultResponse>()
                .with(this)
                .setApiCode(isRecyclerFlow() ? ApiCst.huishouxieyi : ApiCst.jimaixieyi)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        String responseString = "";
                        try {
                            JSONObject object = new JSONObject(response.content);
                            responseString = object.optString("info");
//                            tvProtocol.setText(Html.fromHtml(responseString));
                            if (webView != null) {
                                webView.loadDataWithBaseURL(null, HtmlFormat.getNewContent(responseString), "text/html", "utf-8", null);

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                    }
                })
                .start(new DefaultResponse());
    }

    @Override
    protected void initListener() {

    }

    private boolean isRecyclerFlow() {
        return getIntent().getBooleanExtra(EXTRA_IS_RECYCLER, false);
    }

}
